<template>
  <!-- 卡片 -->
  <el-card class="box-card">
    <!-- 下拉菜单 -->
    <el-form :inline="true" class="demo-form-inline category-selector" :disabled="isDisabled">
      <el-form-item label="一级分类">
        <el-select v-model="category1Id" placeholder="请选择">
          <el-option v-for="c1 in categoryListStore.category1List" :key="c1.id" :label="c1.name" :value="c1.id" />

        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select v-model="category2Id" placeholder="请选择">
        <el-option v-for="c2 in categoryListStore.category2List" :key="c2.id" :label="c2.name" :value="c2.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select v-model="category3Id" placeholder="请选择">
        <el-option v-for="c3 in categoryListStore.category3List" :key="c3.id" :label="c3.name" :value="c3.id" />
        </el-select>
      </el-form-item>

    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "CategorySelector"
}
</script>

<script setup lang="ts">
import { onMounted, computed } from 'vue'

// 引入pinia中创建的categoryListStore
import { useCategoryListStore } from '@/stores/categorySelectorList';
// 注册store
const categoryListStore = useCategoryListStore()

// prop接受数据
defineProps(["isDisabled"])

// 计算属性之一级导航
const category1Id = computed({
  get() {
    return categoryListStore.category1Id as number
  },
  set(id: number) {
    // categoryListStore.category1Id = id
    // 发送请求获取 二级分类 数据
   categoryListStore.getCategory2List(id)
  }
})

// 计算属性之二级导航
const category2Id = computed({
  get() {
    return categoryListStore.category2Id as number
  },
  set(id:number) {
    // 发送请求获取 三级分类 数据
    categoryListStore.getCategory3List(id)
  }
})
// 计算属性之三级导航
const category3Id = computed({
  get() {
    return categoryListStore.category3Id as number
  },
  set(id:number) {
    categoryListStore.setCategory3Id(id)
  }
})


// 生命周期
onMounted(() => {
  // 调用 获取 一级分类下拉列表的方法 渲染页面
  categoryListStore.getCategory1List()
})
</script>

<style scoped lang="scss">
.category-selector {
  .el-form-item {
    margin-bottom: 0;
  }
}
</style>